package jp.android.chiba.gittest;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.simpledb.AmazonSimpleDBClient;
import com.amazonaws.services.simpledb.model.PutAttributesRequest;
import com.amazonaws.services.simpledb.model.ReplaceableAttribute;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Git_testActivity extends Activity {
	private EditText mEdtComment;
	private Button mCmdAdd;
	private static final Integer RADIX = 30;
	private ProgressDialog mProgressDialog;

    /** Called when the activity is first created1. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mEdtComment = (EditText)this.findViewById(R.id.EdtComment);
        mCmdAdd = (Button)this.findViewById(R.id.CmdAdd);

        mCmdAdd.setOnClickListener(CmdAddOnClickListener);

        Log.d("TEST", "Commented by noxi for git test");
        Log.d("TEST", "(´・ω・`)");
        Log.d("TEST", "Hello World");
        Log.d("TEST", "add by noxi for test aaaaa");

		Log.d("YAMAです。",
				"ほげほげほげほげほげほげほげほげほげほげほほげほえｇほげほげほげほげほえっげｈｇほげほげほげほげほげほげほげほげほげほげほほげほえｇほげほげほげほげほえっげｈｇほげほげほげほげほげほげほげほげほげほげほほげほえｇほげほげほげほげほえっげｈｇほげほげほげほげほげほげほげほげほげほげほほげほえｇほげほげほげほげほえっげｈｇ");
        Log.d("TEST", "Commented by kyuji for git test");
        Log.d("TEST", "Commented by kyuji for git test2");
        Log.d("TEST", "Commented by kyuji for git test3");
        Log.d("TEST", "Commented by kyuji for git test4");
        Log.d("TEST", "Commented by kyuji for git test5");

        Log.d("山口","追いついた～＞＜");
        Log.d("saito","再度EGit入れた");
        Log.d("saito","確認の為、再度Push");
        Log.d("TEST", "Commented by noxi for git test0");
        Log.d("nakashima", "EGit test");

        Log.d("aaaa", "aaaa");
    }

    /*
     * コメントの送信
     */
    private OnClickListener CmdAddOnClickListener = new OnClickListener(){
    	// テストコメントを入力 by kazuhiko.uno@gmail.com
		public void onClick(View v) {
			// ダイアログを表示
			mProgressDialog = new ProgressDialog(Git_testActivity.this);
			mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			mProgressDialog.setMessage("Wait...");
			mProgressDialog.show();

			// コメント送信スレッドへ
			(new Thread(runAddComment)).start();
		}
    };

    /**
	* コメント送信スレッド
	*/
	private Runnable runAddComment = new Runnable(){
		public void run() {
			Message msg;
			Resources res;
			AmazonSimpleDBClient sdb;
			PutAttributesRequest request;
			ArrayList<ReplaceableAttribute> atts;

			try{
				res = Git_testActivity.this.getResources();
				// SDB Client作成
				sdb = new AmazonSimpleDBClient(new BasicAWSCredentials(res.getString(R.string.access_key) , res.getString(R.string.secret_key)));
				sdb.setEndpoint(res.getString(R.string.end_point));

				request = new PutAttributesRequest();
				request.withDomainName("TEST");
				request.withItemName(getUniqId());
				atts = new ArrayList<ReplaceableAttribute>();
				atts.add(new ReplaceableAttribute("Comment", mEdtComment.getText().toString(), true));
				request.withAttributes(atts);
				sdb.putAttributes(request);
				handler.sendEmptyMessage(0);
			}catch(Exception e){
				msg = new Message();
				msg.obj = e;
				handler.sendMessage(msg);
			}finally{
				mProgressDialog.dismiss();
			}
		}

		private final Handler handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				if(msg.obj == null){
					Toast.makeText(Git_testActivity.this, "complete!!", Toast.LENGTH_LONG).show();
				}else{
					Toast.makeText(Git_testActivity.this, ((Exception)msg.obj).getMessage(), Toast.LENGTH_LONG).show();
				}
			}
		};
	};

	/**
	 * 一意のキー生成
	 * @return
	 */
	private String getUniqId(){
		long val;
		String id;
		String ds1;
		String ds2;
		Random rand;
		SimpleDateFormat sdf;

		rand = new Random();

		// 年月日時分秒を取得
		sdf = new SimpleDateFormat("yyMMddHHmmss");
		ds1 = sdf.format(new Date());

		// ミリ秒を取得
		sdf = new SimpleDateFormat("S");
		ds2 = String.format("%1$03d", Integer.parseInt(sdf.format(new Date())));
		val = Long.parseLong(ds1 + ds2 + String.format("%1$03d", rand.nextInt(999)));

		// RADIX進で変換する
		id = Long.toString(val, RADIX);

		return id;
	}
}
